// index.js
// 获取应用实例
const app = getApp()
// common.js 
// const {getBanners}=require('../../api/api')
import API from '../../api/api'
let half =0 ;
let quarater = 0;
Page({
  data: {
    banners:[],
    slideOffset:0,
    newsong:[],
    newsong_index:[],
    activeIndex:0,
    songsheet_index:[],
    recommend_MV:[]
  },
  go_search(){
    wx.navigateTo({
      url: '/pages/search/search',
    })
  },
  changeline(e){
    let current=e.detail.current;
    // console.log(current);
    if(current==0){
      this.setData({
        slideOffset:quarater-14,
      })
    }
    else{
      this.setData({
        slideOffset:quarater-14+half
      })
    }
  },
  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  async onLoad() {
    this.resetSlider();
    this.loadBanners();
    this.getNewSong();
    this.songsheet();
    this.getRecommendMV()
    // this.getDjRadios();
    // this.getNewEst();
    // this.getProgramRecommend();
  },
  async getRecommendMV(){
    let res= await API.getRecommendMV();
    if(res.code==200){
      this.setData({
        recommend_MV:res.result.slice(0,4)
      })
    }
  },
  async getNewSong(){
    let res=await API.getNewSong();
    // console.log(song);
    if(res.code==200){  //请求成功
      this.setData({
        newsong: res.result,
        newsong_index:res.result.slice(0,6)
      })      
    }

  },
  async songsheet(){
    let res=await API.getsongsheet({
      order:'hot'
    })
    console.log(res)
    if(res.code==200){
      this.setData({
        songsheet_index:res.playlists.slice(0,6)
      })
    }
  },
  resetSlider(){
    wx.getSystemInfo({
      success: (res) => {
        half = res.windowWidth/2;
        quarater = half/2;
        console.log(half);
        this.setData({
          slideOffset:quarater-14
        })
      },
    })
  },
  async loadBanners(){
    let res=await API.getBanners();
    if(res.code==200){
      this.setData({
        banners:res.banners
      })
    }
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  getUserInfo(e) {
    // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户个人信息
    console.log(e)
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})
